package {

import components.AreaItem;
import components.WorkFieldComponent;


import core.ALevel;
import core.Facade;

import events.DigComponentEvent;
import events.DigLevelEvent;

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;

import ru.cjay.loading.JSONLoader;
import ru.cjay.loading.ResourceManager;
import ru.cjay.loading.SWFAssetList;

import ui.DigLevelUI;
import ui.FlashMessagePopup;
import ui.ShortMessagePopup;
import ui.WinnerInfoPopup;

import utils.TraceObject;

public class DigLevel extends ALevel {


    private var _zone_id:int;
    private var _game_id:int;
    private var _cash:int;
    private var _cash_full:String;
    private var _cash_short:String;
    private var _col:int;
    private var _col_full:String;
    private var _col_short:String;
    private var _jackpot:int;
    private var _jackpot_full:String;
    private var _jackpot_short:String;
    private var _jackpot_hint:String;
    private var _cash_cost:int;
    private var _cash_cost_full:String;
    private var _cash_cost_short:String;
    private var _col_cost:int;
    private var _col_cost_full:String;
    private var _col_cost_short:String;
    private var _prize:Object;
    private var _size_x:int;
    private var _size_y:int;
    private var _winner:Object;
    private var _rating_clicks:Object;
    private var _rating_wins:Object;
    private var _click_result:int;
    private var _next_update:int;
    private var _cells:Array;

    private var dig:WorkFieldComponent;

    private var _caption:String;


    private var _strings_user_count_hint:String;
    private var _strings_user_count:String;
    private var _strings_user_count_suf:String;
    private var _strings_try_cost:String;
    private var _strings_cash_name:String;
    private var _strings_col_name:String;
    private var _strings_in_cash:String;
    private var _strings_in_col:String;
    private var _strings_col_pre:String;
    private var _strings_quit_hint:String;
    private var _strings_click_result_hint:String;
    private var _strings_miss_hint:String;
    private var _strings_tell_friends_hint:String;
    private var _strings_wins_hint:String;
    private var _strings_clicks_hint:String;
    private var _strings_top_hint:String;

    private var _showClickMessage:Boolean = false;

//    private var _selected_monetype:String = "cash";

    private var _updateTimer:Timer;
    private var _isWaiting:Boolean = false;
//    private var prompt:PromptWindow;
//    private var _digComponent:DigComponent;
    private var _ui:DigLevelUI;
    private var _setting:int;

    public function DigLevel(p_area:AreaItem) {
        super();
//        _showClickMessage = true;
        trace(" Area Item is:");
        trace(TraceObject.traceObject(p_area));

        //todo: fill this information RIGHT, fuck!
        _caption = p_area.caption;
        _setting = p_area.setting;
        _cash = p_area.cash;
        _cash_cost = p_area.cash_cost;
        _cash_cost_full = p_area.cash_cost_full;
        _cash_cost_short = p_area.cash_cost_short;
        _cash_full = p_area.cash_full;
        _cash_short = p_area.cash_short;
        _cells = p_area.cells;
        _click_result = p_area.click_result;
        _zone_id = p_area.zone_id;

        _col = p_area.col;
        _col_cost = p_area.col_cost;
        _col_cost_full = p_area.col_cost_full;
        _col_cost_short = p_area.col_cost_short;
        _col_full = p_area.col_full;
        _col_short = p_area.col_short;

        _jackpot = p_area.jackpot;
        _jackpot_full = p_area.jackpot_full;
        _jackpot_short = p_area.jackpot_short;
        _jackpot_hint = p_area.jackpot_hint;

        _size_x = p_area.size_x;
        _size_y = p_area.size_y;
        _winner = p_area.winner;
        _prize = p_area.prize;

        _rating_clicks = p_area.rating_clicks;
        _rating_wins = p_area.rating_wins;
        _click_result = p_area.click_result;
        _next_update = p_area.next_update;

        _strings_user_count_hint = p_area.strings_user_count_hint;
        _strings_user_count = p_area.strings_user_count;
        _strings_user_count_suf = p_area.strings_user_count_suf;
        _strings_try_cost = p_area.strings_try_cost;
        _strings_cash_name = p_area.strings_cash_name;
        _strings_col_name = p_area.strings_col_name;
        _strings_in_cash = p_area.strings_in_cash;
        _strings_in_col = p_area.strings_in_col;
        _strings_col_pre = p_area.strings_col_pre;
        _strings_quit_hint = p_area.strings_quit_hint;
        _strings_click_result_hint = p_area.strings_click_result_hint;
        _strings_miss_hint = p_area.strings_miss_hint;
        _strings_tell_friends_hint = p_area.strings_tell_friends_hint;
        _strings_wins_hint = p_area.strings_wins_hint;
        _strings_clicks_hint = p_area.strings_clicks_hint;
        _strings_top_hint = p_area.strings_top_hint;


        rlist = new Object();
//        rlist["dig_map"] = "http://iamqcenko.appspot.com/static/assets/dig_map.jpg";
//        rlist["small_map_back"] = "http://iamqcenko.appspot.com/static/assets/small_map_back.jpg";

        swf_list = new Array();
        var m_asset:SWFAssetList = new SWFAssetList("main_assets_" + _setting, "http://vd.osa-corp.ru/i/vday/map/settings/" + _setting + "/dig_asset.swf");

        m_asset.addAsset("Sight", "Sight");
        m_asset.addAsset("LabelX", "LabelX");
        m_asset.addAsset("Cell", "Cell");
        m_asset.addAsset("Winner", "Winner");
        m_asset.addAsset("Bang", "Bang");
        m_asset.addAsset("Location", "Location");
        m_asset.addAsset("UIPanel", "UIPanel");
        m_asset.addAsset("Background", "Background");
        swf_list.push(m_asset);
    }

    public function set id(id:int):void {
        _zone_id = id;
    }

    private function exitSession(event:Event):void {
        var url:String = "http://vd.osa-corp.ru/map/quitgame?" + Facade.getInstance().authUrlParams() + "&zone_id=" + _zone_id;
        var loader:JSONLoader = new JSONLoader();
        Facade.getInstance().fadeIn();
        loader.loadJSON(url, function() {
            dispatchEvent(new DigLevelEvent(DigLevelEvent.EXIT_LEVEL));
        });
    }

    private function exitMap(event:Event):void {
        ShortMessagePopup.getInstance().hideMessage();
        if (event.target == event.currentTarget) {
            dispatchEvent(new DigLevelEvent(DigLevelEvent.EXIT_LEVEL));
        }
    }

    private function onDig(event:DigComponentEvent):void {
        ShortMessagePopup.getInstance().hideMessage();
        if (_isWaiting) {
            return;
        }
        if (_updateTimer != null) _updateTimer.stop();
        _isWaiting = true;
        _showClickMessage = true;
        var loader:JSONLoader = new JSONLoader();
        loader.loadJSON("http://vd.osa-corp.ru/map/click?" + Facade.getInstance().authUrlParams() + "&zone_id=" + _zone_id + "&game_id=" + _game_id + "&x=" + event.x + "&y=" + event.y + "&money_type=" + Facade.getInstance().selected_monetype, onMapRefresh);
    }

    private function parse(obj:Object):void {
        _game_id = obj.game_id;
        _cash = obj.cash;
        _cash_full = obj.cash_full;
        _cash_short = obj.cash_short;
        _col = obj.col;
        _col_full = obj.col_full;
        _col_short = obj.col_short;
        _jackpot = obj.jackpot;
        _jackpot_full = obj.jackpot_full;
        _jackpot_short = obj.jackpot_short;
        _jackpot_hint = obj.jackpot_hint;
        _cash_cost = obj.cash_cost;
        _cash_cost_full = obj.cash_cost_full;
        _cash_cost_short = obj.cash_cost_short;

        _col_cost = obj.col_cost;
        _col_cost_full = obj.cost_full;
        _col_cost_short = obj.col_cost_short;
        _prize = obj._prize;
        _size_x = obj._size_x;
        _size_y = obj.size_y;
        _click_result = obj.click_result;
        _next_update = obj.next_update;

        if (obj.cells != null) {
            if (_cells == null) {
                _cells = new Array();
                _cells.length = _size_x;
            }
            for (var row in obj.cells) {
                if (_cells[row] == null) {
                    _cells[row] = new Array();
                    _cells[row].length = _size_y;
                }

                for (var pos_y in (obj.cells[row] as Array)) {
                    _cells[row][pos_y] = obj.cells[row][pos_y];
                }
            }
        }

        if (obj.winner != null) {
            _winner = new Object();
            _winner.user_id = obj.winner.user_id;
            _winner.fio = obj.winner.fio;
            _winner.avatar = obj.winner.avatar;
            _winner.win_date = obj.winner.win_date;
            _winner.win_x = obj.winner.win_x;
            _winner.win_y = obj.winner.win_y;
            _winner.hint = obj.winner.hint;
        }

        if (obj.rating_clicks != null) {
            _rating_clicks = new Array();
            for each(var it:Object in obj.rating_clicks) {
                _rating_clicks.push(it);
            }
        }
        if (obj.rating_wins != null) {
            _rating_wins = new Array();
            for each(var it:Object in obj.rating_wins) {
                _rating_wins.push(it);
            }
        }
    }

    private function onMapRefresh(object:Object):void {
        _isWaiting = false;
        if (object.data.goal == false) {
            if (object.data.code == "no_col") {
                FlashMessagePopup.showFlashMessage(object.data.code);
            }
            if (object.data.code == "no_cash") {
                FlashMessagePopup.showFlashMessage(object.data.code);
            }
        }

        if (object.data.goal == true) {
            // TODO check fow winner;
//            trace("object.data.cells");
            _ui.setJackpot(object.data.jackpot_full);
            trace("MapRefresh count ",object.data.strings.user_count+" "+object.data.strings.user_count_suf);
            _ui.setPlayersCount(object.data.strings.user_count+" "+object.data.strings.user_count_suf);
//            level.strings_user_count+" "+level.strings_user_count_suf;


            if (object.data.winner != null) {
                _winner = new Object();
                _winner.user_id = object.data.winner.user_id;
                _winner.fio = object.data.winner.fio;
                _winner.avatar = object.data.winner.avatar;
                _winner.win_date = object.data.winner.win_date;
                _winner.win_x = object.data.winner.win_x;
                _winner.win_y = object.data.winner.win_y;
                _winner.hint = object.data.winner.hint;
                dig.setWinner(_winner);
                WinnerInfoPopup.getInstance().show(this);

            } else {
                //TODO: add message here
                if (_showClickMessage) {
                    ShortMessagePopup.getInstance().showMessage(_strings_miss_hint);
                    _showClickMessage = false;
                }

                if (_updateTimer == null) _updateTimer = new Timer(_next_update * 1000, 1);
                _updateTimer.reset();
                _updateTimer.start();
            }
            dig.cells = object.data.cells;
        }
    }


    override public function create():void {
        trace("create");
        loadResources(null);
        addEventListener(DigComponentEvent.DIG_IN, onDig);

    }

    override protected function parse_config(config:Object):void {
        trace("____________________ parseConfig", config);

        // TODO: here is my al_al_all
        var background:MovieClip = new (ResourceManager.getInstance().getSwfAssetClass("main_assets_" + this.setting + ":Background") as Class)();
        _ui = new DigLevelUI(this);
        dig = new WorkFieldComponent(_setting);
        dig.background = new (ResourceManager.getInstance().getSwfAssetClass("main_assets_" + this.setting + ":Location") as Class)();
        dig.background.width = 560;
        dig.background.height = 560;
        dig.x = (Facade.WIDTH - dig.background.width) / 2;
        dig.y = 40;
        dig.cells = _cells;
//        _ui.fill(config);
        addChild(background);
        addBasicObject(_ui);
        addChild(dig);


        _ui.ui.btn_enter.addEventListener(MouseEvent.CLICK, exitMap);
        _ui.ui.btn_exit.addEventListener(MouseEvent.CLICK, exitSession);

        trace("Winner 2");
        if (_winner != null) {
//            WinnerInfoPopup.getInstance().show(this);
            updateMap();
            _updateTimer = new Timer(_next_update * 1000, 1);
            _updateTimer.addEventListener(TimerEvent.TIMER, updateMap);

        } else {
//          _updateTimer = new Timer(100, 1);
            _updateTimer = new Timer(_next_update * 1000, 1);
            trace("_next_update = ", _next_update);
            _updateTimer.addEventListener(TimerEvent.TIMER, updateMap);
            trace("timer started");
            _updateTimer.start();
        }
    }

    override public function destroy():void {
        try {
            _updateTimer.removeEventListener(TimerEvent.TIMER, updateMap);
        } catch(e) {

        }
        removeEventListener(DigComponentEvent.DIG_IN, onDig);

        _updateTimer = null;
        super.destroy();

    }

    private function updateMap(evetn:Event = null):void {
        trace("updateMap");
        var loader:JSONLoader = new JSONLoader();
        loader.loadJSON("http://vd.osa-corp.ru/map/gamezone?" + Facade.getInstance().authUrlParams() + "&zone_id=" + _zone_id, onMapRefresh);
//        var request:URLRequest = new URLRequest("http://vd.osa-corp.ru/map/gamezone?" + Facade.getInstance().authUrlParams() + "&zone_id=" + _zone_id);
//        var args:URLVariables = new URLVariables();
//        SafeConnection.getInstance().makeSafe(request, onMapRefresh);
    }

    public function fillFromArea(zone:AreaItem):void {
        _zone_id = zone.zone_id;
        _game_id = zone.zone_id;
        _cash = zone.cash;
        _cash_full = zone.cash_full;
        _cash_short = zone.cash_short;
        _col = zone.col;
        _col_full = zone.col_full;
        _col_short = zone.col_short;
        _jackpot = zone.jackpot;
        _jackpot_full = zone.jackpot_full;
        _jackpot_short = zone.jackpot_short;
        _jackpot_hint = zone.jackpot_hint;
        _cash_cost = zone.cash_cost;
        _cash_cost_full = zone.cash_cost_full;
        _cash_cost_short = zone.cash_cost_short;
        _col_cost = zone.col_cost;
        _col_cost_full = zone.col_cost_full;
        _col_cost_short = zone.col_cost_short;
        _prize = zone.prize;
        _size_x = zone.size_x;
        _size_y = zone.size_y;

        _cells = zone.cells;
        _winner = zone.winner;
        _rating_clicks = zone.rating_clicks;
        _rating_wins = zone.rating_wins;
        _click_result = zone.click_result;
        _next_update = zone.next_update;


    }

    public function get size_x():int {
        return _size_x;
    }

    public function set size_x(value:int):void {
        _size_x = value;
    }

    public function get size_y():int {
        return _size_y;
    }

    public function set size_y(value:int):void {
        _size_y = value;
    }

    public function get cells():Array {
        return _cells;
    }

    public function set cells(value:Array):void {
        _cells = value;
    }

    public function get zone_id():int {
        return _zone_id;
    }

    public function set zone_id(value:int):void {
        _zone_id = value;
    }

    public function get game_id():int {
        return _game_id;
    }

    public function set game_id(value:int):void {
        _game_id = value;
    }

    public function get cash():int {
        return _cash;
    }

    public function set cash(value:int):void {
        _cash = value;
    }

    public function get cash_full():String {
        return _cash_full;
    }

    public function set cash_full(value:String):void {
        _cash_full = value;
    }

    public function get cash_short():String {
        return _cash_short;
    }

    public function set cash_short(value:String):void {
        _cash_short = value;
    }

    public function get col():int {
        return _col;
    }

    public function set col(value:int):void {
        _col = value;
    }

    public function get col_full():String {
        return _col_full;
    }

    public function set col_full(value:String):void {
        _col_full = value;
    }

    public function get col_short():String {
        return _col_short;
    }

    public function set col_short(value:String):void {
        _col_short = value;
    }

    public function get jackpot():int {
        return _jackpot;
    }

    public function set jackpot(value:int):void {
        _jackpot = value;
    }

    public function get jackpot_full():String {
        return _jackpot_full;
    }

    public function set jackpot_full(value:String):void {
        _jackpot_full = value;
    }

    public function get jackpot_short():String {
        return _jackpot_short;
    }

    public function set jackpot_short(value:String):void {
        _jackpot_short = value;
    }

    public function get jackpot_hint():String {
        return _jackpot_hint;
    }

    public function set jackpot_hint(value:String):void {
        _jackpot_hint = value;
    }

    public function get cash_cost():int {
        return _cash_cost;
    }

    public function set cash_cost(value:int):void {
        _cash_cost = value;
    }

    public function get cash_cost_full():String {
        return _cash_cost_full;
    }

    public function set cash_cost_full(value:String):void {
        _cash_cost_full = value;
    }

    public function get cash_cost_short():String {
        return _cash_cost_short;
    }

    public function set cash_cost_short(value:String):void {
        _cash_cost_short = value;
    }

    public function get col_cost():int {
        return _col_cost;
    }

    public function set col_cost(value:int):void {
        _col_cost = value;
    }

    public function get col_cost_full():String {
        return _col_cost_full;
    }

    public function set col_cost_full(value:String):void {
        _col_cost_full = value;
    }

    public function get col_cost_short():String {
        return _col_cost_short;
    }

    public function set col_cost_short(value:String):void {
        _col_cost_short = value;
    }

    public function get prize():Object {
        return _prize;
    }

    public function set prize(value:Object):void {
        _prize = value;
    }

    public function get winner():Object {
        return _winner;
    }

    public function set winner(value:Object):void {
        _winner = value;
    }

    public function get rating_clicks():Object {
        return _rating_clicks;
    }

    public function set rating_clicks(value:Object):void {
        _rating_clicks = value;
    }

    public function get rating_wins():Object {
        return _rating_wins;
    }

    public function set rating_wins(value:Object):void {
        _rating_wins = value;
    }

    public function get click_result():int {
        return _click_result;
    }

    public function set click_result(value:int):void {
        _click_result = value;
    }

    public function get next_update():int {
        return _next_update;
    }

    public function set next_update(value:int):void {
        _next_update = value;
    }

    public function get strings_user_count_hint():String {
        return _strings_user_count_hint;
    }

    public function set strings_user_count_hint(value:String):void {
        _strings_user_count_hint = value;
    }

    public function get strings_user_count():String {
        return _strings_user_count;
    }

    public function set strings_user_count(value:String):void {
        _strings_user_count = value;
    }

    public function get strings_user_count_suf():String {
        return _strings_user_count_suf;
    }

    public function set strings_user_count_suf(value:String):void {
        _strings_user_count_suf = value;
    }

    public function get strings_try_cost():String {
        return _strings_try_cost;
    }

    public function set strings_try_cost(value:String):void {
        _strings_try_cost = value;
    }

    public function get strings_cash_name():String {
        return _strings_cash_name;
    }

    public function set strings_cash_name(value:String):void {
        _strings_cash_name = value;
    }

    public function get strings_col_name():String {
        return _strings_col_name;
    }

    public function set strings_col_name(value:String):void {
        _strings_col_name = value;
    }

    public function get strings_in_cash():String {
        return _strings_in_cash;
    }

    public function set strings_in_cash(value:String):void {
        _strings_in_cash = value;
    }

    public function get strings_in_col():String {
        return _strings_in_col;
    }

    public function set strings_in_col(value:String):void {
        _strings_in_col = value;
    }

    public function get strings_col_pre():String {
        return _strings_col_pre;
    }

    public function set strings_col_pre(value:String):void {
        _strings_col_pre = value;
    }

    public function get strings_quit_hint():String {
        return _strings_quit_hint;
    }

    public function set strings_quit_hint(value:String):void {
        _strings_quit_hint = value;
    }

    public function get strings_click_result_hint():String {
        return _strings_click_result_hint;
    }

    public function set strings_click_result_hint(value:String):void {
        _strings_click_result_hint = value;
    }

    public function get strings_miss_hint():String {
        return _strings_miss_hint;
    }

    public function set strings_miss_hint(value:String):void {
        _strings_miss_hint = value;
    }

    public function get strings_tell_friends_hint():String {
        return _strings_tell_friends_hint;
    }

    public function set strings_tell_friends_hint(value:String):void {
        _strings_tell_friends_hint = value;
    }

    public function get strings_wins_hint():String {
        return _strings_wins_hint;
    }

    public function set strings_wins_hint(value:String):void {
        _strings_wins_hint = value;
    }

    public function get strings_clicks_hint():String {
        return _strings_clicks_hint;
    }

    public function set strings_clicks_hint(value:String):void {
        _strings_clicks_hint = value;
    }

    public function get strings_top_hint():String {
        return _strings_top_hint;
    }

    public function set strings_top_hint(value:String):void {
        _strings_top_hint = value;
    }

    public function get updateTimer():Timer {
        return _updateTimer;
    }

    public function set updateTimer(value:Timer):void {
        _updateTimer = value;
    }

    public function get isWaiting():Boolean {
        return _isWaiting;
    }

    public function set isWaiting(value:Boolean):void {
        _isWaiting = value;
    }

    public function get setting():int {
        return _setting;
    }

    public function set setting(value:int):void {
        _setting = value;
    }

    public function get caption():String {
        return _caption;
    }
}
}
